package com.ukao.steward.util;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.ukao.steward.R;

import static com.ukao.steward.R.drawable.default_head;

/**
 * Created by sunfusheng on 16/4/6.
 */
public class ImageUtils {


    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";


    // 将资源ID转为Uri
    public static Uri resourceIdToUri(Context mContext, int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + mContext.getPackageName() + FOREWARD_SLASH + resourceId);
    }


    /**
     * 加载drawable图片 是否显示默认图片
     *

     */
    public static void loadResImage(Context mContext, int resId, ImageView imageView  ) {
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext, resId))
                    .crossFade()
                    .into(imageView);


    }


    public static void loadHeadImage(Context mContext, String imageurl, ImageView imageView  ) {
        if (!CheckUtils.isEmpty(imageurl) ) {
            Glide.with(mContext)
                    .load(imageurl)
                    .crossFade()
                    .placeholder(default_head)
                    .error(default_head)
                    .into(imageView);
        }else{
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext,R.drawable.default_head))
                    .crossFade()
                    .into(imageView);
        }



    }

    /**
     * 加载本地或者网络资源
     *
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadImage(Context mContext, String path, ImageView imageView) {

        if (!CheckUtils.isEmpty(path) &&path.length()>4&& !path.substring(0, 4).equals("http")) {
            path = "file://" + path;

        }
        if (!CheckUtils.isEmpty(path) ){
            Glide.with(mContext)
                    .load(path)
                    .crossFade()
                    .placeholder(R.drawable.default_head)
                    .error(R.drawable.default_head)
                    .into(imageView);


        } else{
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext, R.drawable.default_head))
                    .placeholder(R.drawable.default_head)
                    .error(R.drawable.default_head)
                    .crossFade()
                    .into(imageView);
        }




    }


    /**
     *加载默认图片
     */
    public static void loadImage(Context mContext, String path, ImageView imageView,int defaultImage) {

        if (!CheckUtils.isEmpty(path) &&path.length()>4&& !path.substring(0, 4).equals("http")) {
            path = "file://" + path;

        }


        if (!CheckUtils.isEmpty(path) ){
            Glide.with(mContext)
                    .load(path)
                    .crossFade()
                    .placeholder(defaultImage)
                    .error(defaultImage)
                    .into(imageView);


        } else{
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext,defaultImage))
                    .crossFade()
                    .into(imageView);
        }




    }


    /**
     * @param mContext
     * @param path
     * @param imageView
     * @param mRequestListener 加载图片回调
     */

    public static void loadImage(Context mContext, String path, ImageView imageView, RequestListener mRequestListener) {

        if (!CheckUtils.isEmpty(path) &&path.length()>4&& !path.substring(0, 4).equals("http")) {
            path = "file://" + path;

        }

        Glide.with(mContext)
                    .load(path)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(mRequestListener)
                    .crossFade()
                    .into(imageView);
        }
    }














